import os
import wx

# Event binding code changed after version 2.5
if wx.VERSION_STRING >= '2.5':
    def bind(actor,event,action,**kw):
        actor.Bind(event,action,**kw)
else:
    def bind(actor,event,action,id=None):
        if id is not None:
            event(actor, id, action)
        else:
            event(actor,action)
            
#basedir = '/Users/chaffra/Projects/LABPLOT/pyAnalysis/iplot/mpl'

def load_bitmap(filename,searchdir):
    """
    Load a bitmap file from the backends/images subdirectory in which the
    matplotlib library is installed or images folder where backend_iplot is installed. The filename parameter should not
    contain any path information as this is determined automatically.

    Returns a wx.Bitmap object
    """

    #basedir = os.path.join(rcParams['datapath'],'images')
    #iplotdir = os.path.join(__file__,'images')
    #print iplotdir
    
    bmpFilename = None
    for dir in searchdir:
        bmpFilename = os.path.normpath(os.path.join(dir, filename))
        if not os.path.exists(bmpFilename):
            continue
        else:
            break
        
    if bmpFilename is None:
        raise IOError('Could not find bitmap file "%s"; dying'%bmpFilename)

    bmp = wx.Bitmap(bmpFilename)
    #img = bmp.ConvertToImage()
    #img.SetSize(wx.Size(30,30))
    return bmp